using CauBeTimMe.objects;
using CauBeTimMe.ultils;
using CauBeTimMe.utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CauBeTimMe.screen
{
    class GameScreen : Screen
    {
        Sprite layer1, layer2, layer3, layer1_cp, layer2_cp, layer3_cp, ground;
        Sprite Boy, spine, fruit;
        Camera2D camera;
        float delta = 0f;
        SpriteFont _spr_font;

        public GameScreen()
        {
            initGame();
        }

        private void initGame()
        {
            //load bg
            layer1 = new Sprite(ImageHelper.LoadImage(@"content/img/bg/layer3.png"));
            layer2 = new Sprite(ImageHelper.LoadImage(@"content/img/bg/layer2.png"));
            layer3 = new Sprite(ImageHelper.LoadImage(@"content/img/bg/layer1.png"));
            layer1_cp = new Sprite(ImageHelper.LoadImage(@"content/img/bg/layer3.png"));
            layer2_cp = new Sprite(ImageHelper.LoadImage(@"content/img/bg/layer2.png"));
            layer3_cp = new Sprite(ImageHelper.LoadImage(@"content/img/bg/layer1.png"));

            ground = new Sprite(ImageHelper.LoadImage(@"content/img/bg/ground.png"));

            layer1.Location = new Vector2(500, 20);
            layer2.Location = new Vector2(0, 20);
            layer3.Location = new Vector2(0, 120);

            layer1_cp.Location = new Vector2(Global.SCREEN_WIDTH + 500, 20);
            layer2_cp.Location = new Vector2(Global.SCREEN_WIDTH, 20);
            layer3_cp.Location = new Vector2(Global.SCREEN_WIDTH, 120);

            ground.Location = new Vector2(-2, 300);

            //load sprite
            Boy = new Sprite(ImageHelper.LoadImage(@"content/img/sprite/boy.png"));
            spine = new Sprite(ImageHelper.LoadImage(@"content/img/sprite/spine.png"));
            fruit = new Sprite(ImageHelper.LoadImage(@"content/img/sprite/fruit.png"));

            Boy.Location = new Vector2(30, 215);
            spine.Location = new Vector2(550, 280);
            fruit.Location = new Vector2(360, 300);

            //camera 
            camera = new Camera2D(Main.getInstance().GraphicsDevice.Viewport);

            _spr_font = Main.getInstance().Content.Load<SpriteFont>("font/SpriteFont1");
        }

        public override void render(SpriteBatch spBatch)
        {

            spBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
            
            Main.getInstance().GraphicsDevice.Clear(Color.LightCyan);

            //draw background
            layer1.draw(spBatch);
            layer1_cp.draw(spBatch);

            layer2.draw(spBatch);
            layer2_cp.draw(spBatch);

            layer3.draw(spBatch);
            layer3_cp.draw(spBatch);

            ground.draw(spBatch);

            //draw sprite
            spine.draw(spBatch);
            fruit.draw(spBatch);
            Boy.draw(spBatch);

            spBatch.End();

            //other screen
            spBatch.Begin();
            spBatch.DrawString(_spr_font, string.Format("Delta={0}", delta),
             new Vector2(100.0f, 20.0f), Color.Red);
            spBatch.End();
        }

        public override void update(GameTime gameTime)
        {
            //update camera
            camera.Update();

            //config these layer follow camera position
            camera.Pos = new Vector2(camera.Pos.X - Global.CAMERA_SCROLL_SPEED, camera.Pos.Y);
            layer1.Location += new Vector2(Global.CAMERA_SCROLL_SPEED - Global.CAMERA_SCROLL_SPEED * Global.LAYER1_SCROLL_SPEED, 0);
            layer2.Location += new Vector2(Global.CAMERA_SCROLL_SPEED - Global.CAMERA_SCROLL_SPEED * Global.LAYER2_SCROLL_SPEED, 0);
            layer3.Location += new Vector2(Global.CAMERA_SCROLL_SPEED - Global.CAMERA_SCROLL_SPEED * Global.LAYER3_SCROLL_SPEED, 0);

            layer1_cp.Location += new Vector2(Global.CAMERA_SCROLL_SPEED - Global.CAMERA_SCROLL_SPEED * Global.LAYER1_SCROLL_SPEED, 0);
            layer2_cp.Location += new Vector2(Global.CAMERA_SCROLL_SPEED - Global.CAMERA_SCROLL_SPEED * Global.LAYER2_SCROLL_SPEED, 0);
            layer3_cp.Location += new Vector2(Global.CAMERA_SCROLL_SPEED - Global.CAMERA_SCROLL_SPEED * Global.LAYER3_SCROLL_SPEED, 0);

            ground.Location += new Vector2(Global.CAMERA_SCROLL_SPEED, 0);

            //condition of these layer
            if (layer1_cp.Location.X <= Math.Abs(camera.Pos.X) - Global.SCREEN_WIDTH)
            {
                layer1_cp.Location += new Vector2(2*Global.SCREEN_WIDTH, 0);
            }
            if (layer2_cp.Location.X <= Math.Abs(camera.Pos.X) - Global.SCREEN_WIDTH)
            {
                layer2_cp.Location += new Vector2(2 * Global.SCREEN_WIDTH, 0);
            }
            if (layer3_cp.Location.X <= Math.Abs(camera.Pos.X) - Global.SCREEN_WIDTH)
            {
                layer3_cp.Location += new Vector2(2 * Global.SCREEN_WIDTH, 0);
            }
            //
            if (layer1.Location.X <= Math.Abs(camera.Pos.X) - Global.SCREEN_WIDTH)
            {
                layer1.Location += new Vector2(2 * Global.SCREEN_WIDTH, 0);
            }
            if (layer2.Location.X <= Math.Abs(camera.Pos.X) - Global.SCREEN_WIDTH)
            {
                layer2.Location += new Vector2(2 * Global.SCREEN_WIDTH, 0);
            }
            if (layer3.Location.X <= Math.Abs(camera.Pos.X) - Global.SCREEN_WIDTH)
            {
                layer3.Location += new Vector2(2 * Global.SCREEN_WIDTH, 0);
            }

            //player
            Boy.Location += new Vector2(Global.CAMERA_SCROLL_SPEED, 0);
        }

        public override void touchEvent(TouchLocation touch)
        {

        }

        public override void touchEvent(GestureSample gesture)
        {
            if (gesture.GestureType == GestureType.VerticalDrag)
            {
                
            }
            if (gesture.GestureType == GestureType.DragComplete)
            {
                delta = gesture.Delta.Y;
                if (gesture.Delta.Y > 20)
                {
                    Boy.Location += new Vector2(0, 50);
                }
                if (gesture.Delta.Y < -20)
                {
                    Boy.Location -= new Vector2(0, 50);
                }
            }
        }
    }
}
